<?php 

namespace Home\Controller;
use Think\Controller;
use Common\Common\Tree;
/**
* 个人展示,功能区
*/
class PeopleController extends Controller
{


	public function index(){
	//个人展示中心
		$id = !empty($_GET['id']) ? $_GET['id'] : $_SESSION['id'];
		
		if(!empty($_GET['id'])){
			//判断是否是本人访问自己的主页
			if ($_GET['id'] != $_SESSION['id']) {

				$sql = "UPDATE `user` SET `index` = `index`+'1' where `id` = '{$_GET['id']}'";
				$modle = M('user');
				$result = $modle -> execute($sql);
				$status = 1;
			}else{

				$status = 0;
			}	
		}else{

			$status = 0;
		}
		//查询数据
		$sql = "SELECT
					`city`,
					`province`,
					`score`,
					`index`,
					`username`
				FROM
					`user`
				WHERE
				id = '{$id}'
				";
		$modle = M();
		$result = $modle -> query($sql);
		$result = current($result);
		$result['status'] = $status;
		$sql = "SELECT
					w.company,
					w.job,
					w.start,
					w.end					
				FROM
					`work` AS w
				LEFT JOIN `user` AS a ON a.id = w.uid
				WHERE
				a.id = '{$id}'
				";
		$work = $modle -> query($sql);
		$result['work'] = $work;

		$sql = "SELECT
					e.school,
					e.profession,
					e.year
				FROM
					`education` AS e
				RIGHT JOIN `user` AS a ON a.id = e.uid
				WHERE
				a.id = '{$id}'
				";
		$education = $modle -> query($sql);
		$result['education'] = $education;

		$sql = "SELECT
					b.title,
					b.content,
					b.id,
					b.time,
					b.views,
					count(c.id) as focus_num
				FROM
					`user` AS a
				LEFT JOIN `question` AS b ON a.id = b.uid
				LEFT JOIN `question_focus` AS c ON c.qid = b.id
				LEFT join `answer` AS d ON d.qid = b.id
				WHERE
				a.id = '{$id}'
				GROUP BY 
				b.id
				";
		$problem = $modle -> query($sql);
		$sql = "SELECT
					count(d.id) as answer_num
				FROM
					`user` AS a
				LEFT JOIN `question` AS b ON a.id = b.uid
				LEFT JOIN `answer` AS d ON d.qid = b.id
				WHERE
					a.id = '{$id}'
				GROUP BY
					b.id
				";

		$answer = $modle -> query($sql);
		$length = count($problem);
		for ($i=0; $i < $length; $i++) { 
			$problem[$i]['answer_num'] = $answer[$i]['answer_num'];
		}


		if($problem[0]['title'] != null){
			$prev =  "/[?]{3,}$/";
			foreach ($problem as $value) {

				$check = preg_match($prev, $value['title']);
				if ($check) {
					$result['question'][] = $value;
				}else{
					$result['article'][] = $value;
				}
			}
		}

		$sql = "SELECT
					b.content,
					b.time,
					b.agree,
					c.title,
					c.id
				FROM
					`user` AS a
				LEFT JOIN `answer` AS b on a.id = b.uid
				LEFT JOIN `question` AS c on c.id = b.qid
				WHERE
				a.id = '{$id}'
				";
		$answer = $modle -> query($sql);
		$result['answer'] = $answer;
		$result['answer_num'] = count($result['answer']);
		$result['question_num'] = count($result['question']);
		$result['article_num'] = count($result['article']);	
		var_dump($result);
		// $result = current($result);	
		$this->assign('result',$result);
		// $result = null;			
		$this->display();	
	}

	public function article(){
	//发布文章
		$sql = "SELECT * from `category`";

		$category = M('category');

		$result = $category -> query($sql);

		$tree = new Tree();

		$result = $tree->index($result);

		$this->assign('result',$result);

		$sql = "SELECT * from `theme`";

		$theme = M('theme');

		$res = $theme-> query($sql);

		$this->assign('res',$res);
					
		$this->display();	
	}

	public function add_article(){	
		
		if(IS_POST){
			
			if (I('post.category_id')==''||I('post.editorValue')==''||I('post.title')=='') {
				$this->error('标题,内容,分类不能为空','/Home/People/article',1);
			}

			$data['category_id'] = I('post.category_id');
			$data['content'] = I('post.editorValue');
			$data['title'] = I('post.title');
			$data['uid'] = I('session.id');
			$data['time'] = date('Y-m-d h:m:s',time());

			$this->add($data);
			// $sql = "INSERT INTO `question`(`title`, `content`, `time`, `category_id`, `uid`)VALUES('{$title}', '{$content}', '{date('Y-m-d h:m:s',time())}', '{$category_id}', '{$uid}')";
		}
	}

	public function question(){
	//提出问题
		$sql = "SELECT * from `category`";

		$modle = M('category');

		$result = $modle -> query($sql);

		$tree = new Tree();

		$result = $tree->index($result);

		$this->assign('result',$result);

		$sql = "SELECT * from `theme`";

		$theme = M('theme');

		$res = $theme-> query($sql);

		$this->assign('res',$res);
					
		$this->display();
				
	}
	public function add_question(){
			
		if(IS_POST){
			
			if (I('post.category_id')==''||I('post.editorValue')==''||I('post.question_content')=='') {
				$this->error('标题,内容,分类不能为空','/Home/People/question',1);
			}

			$data['category_id'] = I('post.category_id');
			$data['content'] = I('post.editorValue');
			$data['title'] = I('post.question_content').'???';
			$data['uid'] = I('session.id');
			$data['time'] = date('Y-m-d h:m:s',time());

			$this->add($data);
			// $sql = "INSERT INTO `question`(`title`, `content`, `time`, `category_id`, `uid`)VALUES('{$title}', '{$content}', '{date('Y-m-d h:m:s',time())}', '{$category_id}', '{$uid}')";
		}
	}

	public function article_show(){

		if(!empty($_GET)){

			$this->display();
		}else{
			var_dump(1111111111);
			$this->display('Help:index');
		}

		
	}

	public function article_edit_show(){

		$this->display();	
	}


	private function add($data){

		$model = M('question');
			$result = $model->add($data);
			$model = M('question_theme');
			if (!empty($_POST['theme_id'])) {

				foreach ($_POST['theme_id'] as $value) {

					$sql = "INSERT INTO `question_theme`(`qid`, `tid`)VALUES('{$result}', '{$value}')";
					$model->execute($sql);
				}				
			}
		$this->success('提问成功','',2);
	}


	public function ueditor(){
        $data = new \Org\Util\Ueditor();
        echo $data->output();
    }


	public function mail(){

		//还要添加权限，如果字段里面的email已经是开启状态，那么就不能执行该方法
        SendMail('1207044909@qq.com','test123','学并思测试邮件');

	}

}
?>